/*
 * ShowDocPanel.java
 *
 * Created on 28 December 2008, 09:13
 */

package srl.gui;

import java.awt.Color;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import srl.tools.struct.Counter;
import org.apache.lucene.analysis.Token;
import srl.corpus.BeginTagToken;
import srl.corpus.Corpus;
import srl.corpus.EndTagToken;
import srl.corpus.SrlDocument;

/**
 *
 * @author  john
 */
public class ShowDocPanel extends javax.swing.JPanel {
    
    public static int TEXT = 0;
    public static int TAGGED = 1;
    public static int TEMPLATES = 2;
    
    static Counter<String> nameID = new Counter<String>();
    
    final String name;
    
    /** Creates new form ShowDocPanel */
    public ShowDocPanel(String docName, TreeSet<DocHighlight> highlights, int mode, String msg) {
        initComponents();
        jTextField1.setText(docName);
        Corpus corpus = SRLGUIApp.getApplication().proj.corpus;
        StyledDocument styledDoc = jTextPane1.getStyledDocument();
        Style defaultStyle = styledDoc.addStyle("default", null);
        try {
            List<String> doc;
            if(mode == TEXT) {
                doc = corpus.getDocSentences(docName);
            } else if(mode == TAGGED) {
                doc = corpus.getDocTaggedContents(docName);
            } else if(mode == TEMPLATES) {
                doc = corpus.getDocTemplateExtractions(docName);
            } else {
                throw new IllegalArgumentException("unrecognized mode");
            }
            Iterator<DocHighlight> hlIter = highlights.iterator();
            DocHighlight dhl = hlIter.hasNext() ? hlIter.next() : null;
            int index = 0;
            int sentenceNo = 0;
            for(String sent : doc) {
                SrlDocument srlDoc = new SrlDocument("null", sent, corpus.getProcessor());
                Iterator<Token> tkIter = srlDoc.iterator();
                int i = 0;
                Token tk = tkIter.next();
                while(dhl != null && dhl.sentence == sentenceNo) {
                    while(i < dhl.beginToken || tk instanceof EndTagToken) {
                        String tkTxt = tokenText(tk) + " ";
                        styledDoc.insertString(index, tkTxt, defaultStyle);
                        index += tkTxt.length();
                        if(mode != TAGGED || !(tk instanceof BeginTagToken || tk instanceof EndTagToken))
                           i++;
                        tk = tkIter.next();
                    }
                    Style style = styledDoc.addStyle(sentenceNo + " " + dhl.beginToken, null);
                    StyleConstants.setBold(style, true);
                    StyleConstants.setForeground(style, dhl.colour);
                    while(i < dhl.endToken || tk instanceof EndTagToken) {
                        String tkTxt = tokenText(tk) + " ";
                        styledDoc.insertString(index, tkTxt, style);
                        index += tkTxt.length();
                        if(mode != TAGGED || !(tk instanceof BeginTagToken || tk instanceof EndTagToken))
                           i++;
                        tk = tkIter.next();
                    }
                    while(dhl != null && dhl.sentence == sentenceNo &&
                            dhl.endToken <= i)
                        dhl = hlIter.hasNext() ? hlIter.next() : null;
                }
                while(true) {
                    String tkTxt = tokenText(tk) + " ";
                    styledDoc.insertString(index, tkTxt, defaultStyle);
                    index += tkTxt.length();
                    if(!tkIter.hasNext())
                        break;
                    tk = tkIter.next();
                }
                styledDoc.insertString(index, "\n", defaultStyle);
                index++;
                
                sentenceNo++;
            }
        } catch(Exception x) {
            x.printStackTrace();
        } 
        if(!nameID.contains(docName + " (" + msg + ")"))
            name = docName + " (" + msg + ")";         
        else
            name = docName + " (" + msg + " " + (nameID.get(docName + " (" + msg + ")") + 1) + ")";
        nameID.add(docName + " (" + msg + ")");
    }
    
    public static String tokenText(Token t) {
        if(t instanceof BeginTagToken) {
            return ((BeginTagToken)t).getTag();
        } else if(t instanceof EndTagToken) {
            return ((EndTagToken)t).getTag();
        } else {
            return t.termText();
        }
    }
    
   
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(srl.gui.SRLGUIApp.class).getContext().getResourceMap(ShowDocPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextField1.setEditable(false);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextPane1.setEditable(false);
        jTextPane1.setName("jTextPane1"); // NOI18N
        jScrollPane1.setViewportView(jTextPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
    
}
